/*
 * AlgebraInfo.java
 *
 * Created on 18 mei 2007, 15:18
 *
 * This file is part of SimpLie.
 * 
 * SimpLie is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SimpLie is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SimpLie.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package edu.simplie.ui;

import edu.simplie.dynkindiagram.*;
import edu.simplie.algebra.*;
import edu.simplie.*;

import javax.swing.table.*;
import java.util.Collection;
import java.util.Iterator;


/**
 * Displays information on a composite of algebras.
 * The information contains the (symmetrized / inverse) Cartan matrix, 
 * the root space metric, the root system, and arbitrary highest
 * weight representations.
 * 
 * @see		AlgebraComposite
 * @author  Teake Nutma
 * @version $Revision: 435 $, $Date: 2009-09-17 10:15:03 +0000 (Thu, 17 Sep 2009) $
 */
public class AlgebraInfo extends javax.swing.JPanel implements DiagramListener
{
	private DefaultTableModel tableModelRoots;
	private Algebra algebra;
	private AlgebraComposite algebras;
	
	/** Creates new form AlgebraInfo */
	public AlgebraInfo()
	{
		initComponents();
		tableModelRoots = (DefaultTableModel) rootTable.getModel();
		algebra = null;
	}
	
	/** Sets the composite of algebras we want to view info of. */
	public void setAlgebraComposite(AlgebraComposite algebras)
	{
		this.algebras = algebras;
		algebras.dd.addListener(this);
	}
	
	public void diagramChanged()
	{
		switch(algebrasBox.getSelectedIndex())
		{
		case 0:
			algebra = algebras.algebra;
			break;
		case 1:
			algebra = algebras.subAlgebra;
			break;
		case 2:
			algebra = algebras.intAlgebra;
			break;
		default:
			algebra = null;
		}
		
		if(algebra == null)
			return;

		uIAlgebraInfo.update(algebra);

		String matrix;
		switch(matrixBox.getSelectedIndex())
		{
			case 0:
				matrix = Helper.matrixToString(algebra.A);
				break;
			case 1:
				matrix = Helper.matrixToString(algebra.B);
				break;
			case 2:
				matrix = Helper.matrixToString(algebra.invA);
				break;
			case 3:
				matrix = Helper.matrixToString(algebra.G);
				break;
			default:
				matrix = "";
		}
		tfMatrix.setText(matrix);

		if(tableContainer.isVisible())
			fillRootTable();
	}

	private void fillRootTable()
	{
		if(algebra == null)
			return;

		// Clear and fill the table.
		tableModelRoots.setRowCount(0);

		int maxHeight = heightSpinner.getValue();
		algebra.rs.construct(maxHeight);
		maxHeight = (maxHeight == 0) ? algebra.rs.size() : Math.min(algebra.rs.size(),maxHeight+1);
		for (int i = 1; i < maxHeight; i++)
		{
			Collection roots	= algebra.rs.get(i);
			Iterator iterator	= roots.iterator();
			while (iterator.hasNext())
			{
				Root root = (Root) iterator.next();
				Object[] rowData = new Object[6];
				rowData[0] = Helper.arrayToString(root.vector);
				rowData[1] = Helper.arrayToString(algebra.rootToWeight(root.vector));
				rowData[2] = root.norm;
				rowData[3] = root.mult;
				rowData[4] = root.coMult;
				rowData[5] = root.height();
				tableModelRoots.addRow(rowData);
			}
		}
	}
	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();
        matricesContainer = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tfMatrix = new javax.swing.JTextArea();
        tableContainer = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        rootTable = new edu.simplie.ui.reusable.UIPrintableColorTable();
        jLabel1 = new javax.swing.JLabel();
        heightSpinner = new edu.simplie.ui.reusable.UISpinner();
        uIAlgebraInfo = new edu.simplie.ui.reusable.UIAlgebraInfo();
        jPanel2 = new javax.swing.JPanel();
        algebrasBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        matrixBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();

        tabbedPane.setMinimumSize(new java.awt.Dimension(0, 0));

        tfMatrix.setColumns(20);
        tfMatrix.setRows(5);
        jScrollPane1.setViewportView(tfMatrix);

        org.jdesktop.layout.GroupLayout matricesContainerLayout = new org.jdesktop.layout.GroupLayout(matricesContainer);
        matricesContainer.setLayout(matricesContainerLayout);
        matricesContainerLayout.setHorizontalGroup(
            matricesContainerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(matricesContainerLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE)
                .addContainerGap())
        );
        matricesContainerLayout.setVerticalGroup(
            matricesContainerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, matricesContainerLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabbedPane.addTab("Matrices", matricesContainer);

        tableContainer.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                tableContainerComponentShown(evt);
            }
        });

        rootTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Vector", "Dynkin labels", "Norm", "Multiplicity", "CoMultiplicity", "Height"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Long.class, java.lang.Long.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        rootTable.setColumnSelectionAllowed(true);
        jScrollPane2.setViewportView(rootTable);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(edu.simplie.SimpLieApp.class).getContext().getResourceMap(AlgebraInfo.class);
        jLabel1.setText(resourceMap.getString("algebraInfo.maxHeight")); // NOI18N

        heightSpinner.setMinValue(0);
        heightSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                heightSpinnerStateChanged(evt);
            }
        });

        org.jdesktop.layout.GroupLayout tableContainerLayout = new org.jdesktop.layout.GroupLayout(tableContainer);
        tableContainer.setLayout(tableContainerLayout);
        tableContainerLayout.setHorizontalGroup(
            tableContainerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(tableContainerLayout.createSequentialGroup()
                .addContainerGap()
                .add(tableContainerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE)
                    .add(tableContainerLayout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(heightSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 56, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        tableContainerLayout.setVerticalGroup(
            tableContainerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(tableContainerLayout.createSequentialGroup()
                .addContainerGap()
                .add(tableContainerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(heightSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabbedPane.addTab("Positive roots", tableContainer);

        uIAlgebraInfo.setTitle(""); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        algebrasBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Full algebra", "Regular subalgebra", "Internal algebra" }));
        algebrasBox.setToolTipText(resourceMap.getString("algebraInfo.selectTooltip")); // NOI18N
        algebrasBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                algebrasBoxActionPerformed(evt);
            }
        });

        jLabel3.setText("Select algebra:"); // NOI18N
        jLabel3.setToolTipText(resourceMap.getString("algebraInfo.selectTooltip")); // NOI18N

        matrixBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cartan matrix", "Root space metric", "Inverse of Cartan matrix", "Quadratic form matrix" }));
        matrixBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                matrixBoxActionPerformed(evt);
            }
        });

        jLabel4.setText("Select matrix:");

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel3)
                    .add(jLabel4))
                .add(10, 10, 10)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(matrixBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(algebrasBox, 0, 222, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(algebrasBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(matrixBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4))
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(tabbedPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(uIAlgebraInfo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(uIAlgebraInfo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(tabbedPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
				
	private void algebrasBoxActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_algebrasBoxActionPerformed
	{//GEN-HEADEREND:event_algebrasBoxActionPerformed
		diagramChanged();
}//GEN-LAST:event_algebrasBoxActionPerformed
	
	private void matrixBoxActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_matrixBoxActionPerformed
	{//GEN-HEADEREND:event_matrixBoxActionPerformed
		diagramChanged();
		tabbedPane.setSelectedIndex(0);
	}//GEN-LAST:event_matrixBoxActionPerformed

	private void tableContainerComponentShown(java.awt.event.ComponentEvent evt)//GEN-FIRST:event_tableContainerComponentShown
	{//GEN-HEADEREND:event_tableContainerComponentShown
		fillRootTable();
}//GEN-LAST:event_tableContainerComponentShown

	private void heightSpinnerStateChanged(javax.swing.event.ChangeEvent evt)//GEN-FIRST:event_heightSpinnerStateChanged
	{//GEN-HEADEREND:event_heightSpinnerStateChanged
		fillRootTable();
	}//GEN-LAST:event_heightSpinnerStateChanged
	
	
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox algebrasBox;
    private edu.simplie.ui.reusable.UISpinner heightSpinner;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel matricesContainer;
    private javax.swing.JComboBox matrixBox;
    private edu.simplie.ui.reusable.UIPrintableColorTable rootTable;
    private javax.swing.JTabbedPane tabbedPane;
    private javax.swing.JPanel tableContainer;
    private javax.swing.JTextArea tfMatrix;
    private edu.simplie.ui.reusable.UIAlgebraInfo uIAlgebraInfo;
    // End of variables declaration//GEN-END:variables
	
	}
